<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Link extends VNT_Controller{
		function Link(){
			parent::VNT_Controller();
			$this->load->library('xajax', 'category');
			$this->xajax->registerFunction(array('test', &$this, 'test')); 
			
			$this->xajax->processRequest();
			$this->xajax_js = $this->xajax->getJavascript($this->base_url.'js/');
			$this->load->model('link_model', 'link', true);
			
			$this->load->helper('form');
			
			$this->checkLogin();
		}
		
		function index(){
			//echo $this->config->item('language');
			//$this->main = '<div> erer erer er </div>';
			//$this->execute();
			//$this->load->view('main', '');
			$this->view();
		}
		
		function create(){
			$data = array();
			$data['id'] = 0;
			$data['name'] = '';
			$data['link_to'] = '';
			$data['name'] = '';
			$data['position'] = '';
			$data['active'] = 1;
			$data['error_message'] = '';
			$data['legend_text'] = lang('create_link');
			$this->main = $this->load->view('admin/linkweb/edit_link', $data, true);
			$this->execute();
		}
		
		
		function edit($id){
			$link = $this->link->getById($id);
			
			if(!$link){
				redirect('admin/link/view');
			}
						
			$data = array();
			$data['id'] = $link->id_link;
			$data['name'] = $link->name;
			$data['link_to'] = $link->link_to;
			$data['order_view'] = $link->order_view;
			$data['active'] = $link->active;
			$data['error_message'] = '';
			$this->main =  $this->load->view('admin/linkweb/edit_link', $data, true);
			$this->execute();
		}
		
		function view(){
			
			$NUMBER_RECORD_PER_PAGE = 15;
			$this->load->library('pagination');
			//$parent = $this->uri->segment(4, 0);
			$page = (int)$this->uri->segment(4);
			$config['base_url'] = $this->base_url . 'admin/link/view/';
			$config['total_rows'] = $this->link->getAllCount();
			$config['per_page'] = $NUMBER_RECORD_PER_PAGE;
			$config['uri_segment'] = 4;
			$config['first_link'] = 'First';
			
			$this->pagination->initialize($config);
							
			$rows = $this->link->getAll('ASC', $page, $NUMBER_RECORD_PER_PAGE);
			$data = array();
			$data['rows'] = $rows;
			$this->main = $this->load->view('admin/linkweb/links_list', $data, true);
			
			$this->execute();
		}
		
		
		function save() {
			$id = (int) $this->input->post('txtID');
			//echo $active = $this->input->post('active');
			$link = new stdClass();
			if($id > 0){
				$link = $this->link->getById($id);
				if(!$link){
					redirect('admin/link');
					return;
				}
			}
			else{
				$link->id_link = 0;
				
			}
		
			$link->name = trim($this->input->post('txtName'));
			$link->link_to = trim($this->input->post('txtLinkTo'));
		
			$link->active = (int)($this->input->post('active'));
			//$adv->id_parent_tab = trim($this->input->post('id_parent'));
		
		
			$error = false;
			$error_message = '';

			//$this->load->helper(array('form', 'url'));
		
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('txtName', 'lang:name', 'required|min_length[4]|max_length[150]');
			$this->form_validation->set_rules('txtLinkTo', 'lang:link_to', 'required|prep_url');
					
									
			if ($this->form_validation->run() == FALSE){
				$data = array();
				
				$data['id'] = $link->id_link;
				
				$data['name'] = $link->name;
				
				$data['link_to'] = $link->link_to;
				
				$data['active'] = $link->active;
				
				$data['error_message'] = '<div style="color:red">' . validation_errors(). '</div>';
			
				$data['legend_text'] = $id > 0 ?  lang('edit_link') : lang('create_link');
						
				$this->main = $this->load->view('admin/linkweb/edit_link', $data, true);
				
				$this->execute();
			}
		else{
							
			if($id > 0){	
				//$adv->active = (int)($this->input->post('active1'));			
				$result = $this->link->update($link);
			}
			else{
				$this->link->name = $link->name;
				$this->link->link_to = $link->link_to;
				$this->link->order_view = $this->link->getMaxOrder() + 1;
				$this->link->active = $link->active;
				
				$id_link = $this->link->insert();
									
			}
				
			redirect('admin/link/view/');
		}
		
	}
	
	function change_order(){
		$id1 = (int)$this->uri->segment(4);
		$id2 = (int)$this->uri->segment(5);
		
		$link1 = $this->link->getById($id1);
		$link2 = $this->link->getById($id2);
		
		if($link1 AND $link2){
			$position1 = $link1->order_view;
			$link1->order_view = $link2->order_view;
			$link2->order_view = $position1;
			
			$this->link->update($link1);
			$this->link->update($link2);
			
		}
		
		redirect('admin/link/');
	}

		
		function upload_image($field_name, $path)
		{
			$result = array();
			$config['upload_path'] = $path; //'./Data/Images/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			//$config['max_size']	= '1000';
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['overwrite']  = true;
			
			
			$this->load->library('upload', $config);
		
			if (!$this->upload->do_upload($field_name))
			{
				//$error = array('error' => $this->upload->display_errors());
				$result['error'] = true;
				$result['message'] = $this->upload->display_errors();
				//$this->load->view('upload_form', $error);
			}	
			else
			{
				//$data = array('upload_data' => $this->upload->data());
				$result['error'] = false;
				$result['upload_data'] = $this->upload->data();
			}
			
			return $result;
		}	
		
				
		function friendly_url_check($url){
			$result = $this->news->getByFriendlyUrl($url);
			if ($result!=null){ 
				$this->form_validation->set_message('friendly_url_check', '%s is not available.');
				return FALSE;
			}
			else{
				return TRUE;
			}
		}
		
				
		function testGetFile(){
 		$config = array(
 			'allowed' => array('JPEG', 'jpg', 'PNG', 'png', 'GIF', 'gif'),
 			'max_size'=> 1024000000 /*1Mb*/
 		);
 		
 		 $result = getFile($config, 'Data', 'http://images8.dantri.com.vn/ThumbImages/Uploaded/2009/05/08/lut080520094_327.jpg');
 		 
 		 if(!$result['error']) echo 'Get file success.';
 		 else echo $result['message'];
 		 
 	}
 	
 	function delete(){
 		$id = intval($this->uri->segment(4));
		$ids = array();
 		if($id>0){
			$ids[] = (int)$id;
 			//$this->product->delete($id);
		}
 		else{
 			if(($this->input->post('submitDelcategory'))){
 				$ids = $this->input->post('categoryBox');
 			}
 		}
 		
		if(count($ids)){
	
 			$result = $this->link->delete($ids);
						
		}
		
 		redirect('admin/link/');
 	}
 	
	}
?>
